<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Modules\Blog\Controllers
 */
class Admin extends Admin_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->db->dbprefix="l_";
		$this->load->model('settle_log_m');
		$this->load->model('channel_m');
		$this->load->helper('url');
		$this->lang->load('settle');
		$this->template
		->append_css('module::settle.css')
		->append_css('libs/960_24_col.css')
		->append_css('libs/daterangepicker.css')
		->append_js('libs/jquery.extennsion.js')
		->append_js('libs/date.js')
		->append_js('libs/daterangepicker.js')
		->append_js('module::settle.js');
	}

	/**
	 * Show all created blog posts
	 */
	public function index()
	{
		$this->load->helper('form');

		// Params
		$fromdate =$this->input->post('fromdate');
		$todate =$this->input->post('todate');

		$fromdate = $fromdate?$fromdate:date('Y-m-d');
		$todate = $todate?$todate:date('Y-m-d');

		$metaModel=array(
			"fromdate"=>$fromdate,
			"todate"=>$todate
		);

		$settleLogModel =new Settle_Log_m();
		$logList = $settleLogModel->getByDateRange($fromdate, $todate);

		$this->template
		->set('logList',$logList)
		->set('metaModel',$metaModel)
		->build('admin/index');
	}
	
	public function settle(){
		
		$open_date = isset($_REQUEST['open_date'])?date_create_from_format('Y-m-d',$_REQUEST['open_date']):(new DateTime());
		$dayOfWeek=$open_date->format("N");
		$channelModel =new Channel_m();
		$channelList = $channelModel->getChannelListByDay($dayOfWeek);
		$this->template
		->set('channelList',$channelList)
		->set('open_date',$open_date)
		->build('admin/settle');
	}
	
	public function crawl()
	{
		$this->load->helper('form');
		$open_date = isset($_REQUEST['open_date'])?date_create_from_format('Y-m-d',$_REQUEST['open_date']):(new DateTime());
		$channel_id_list = $this->input->post('channel_id');
		$channel_id_list=$channel_id_list?$channel_id_list:'';
		$channel_id_list=implode(";", $channel_id_list);
		$url=base_url().'cron/settle/settle.php?send_mail=false&open_date='
			.$open_date->format('Y-m-d')
			."&channel_id_list="
			.$channel_id_list;
		redirect($url);
	}
}
